package Actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.filechooser.FileNameExtensionFilter;

import Model.Author;
import Model.Manuscript;
import TableModels.AuthorTemplate;

public class UpdatePaper extends AbstractAction{
	JTable table;
	Author author;
	
	public UpdatePaper(JTable table, Author author) {
		this.table = table;
		this.author = author;
		this.putValue(NAME, "Update");
		this.putValue(MNEMONIC_KEY, KeyEvent.VK_D);
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(table.getSelectedRow() != -1) {
			AuthorTemplate model = (AuthorTemplate)table.getModel();
			//convert to models row index, since the table index changes when it is sorted
			int row = table.convertRowIndexToModel(table.getSelectedRow());
			
			//bring up a file chooser, save new file
			String link;
			String title;
			
			//save old manuscript
			Manuscript oldM = model.getManuscript(row);  //store for using as a parameter
			Manuscript newM = null;			
			
			
			JFileChooser chooser = new JFileChooser();
		    FileNameExtensionFilter filter = new FileNameExtensionFilter(
		        "docx files", "docx");
		    chooser.setFileFilter(filter);
		    int returnVal = chooser.showOpenDialog(null);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		    	title = JOptionPane.showInputDialog("Enter Title");  //get title from user
		    				//tell the model to submit the paper, a confirmation is returned
		    	link = "/Papers/" + chooser.getSelectedFile().getName();
		    	newM = author.modifyPaper(oldM, title, link);
		    	if(newM != null) {
		    		copyFile(chooser.getSelectedFile());
		    		model.setManuscript(newM, row);   //copy new manuscript to tablemodel
		    		if(!link.equals(oldM.getLink())) { //if files names are different
		    			//delete old link
		    			File file = new File("src/" + oldM.getLink());
		    			file.delete();
		    		}
		    		table.repaint();
		    		JOptionPane.showMessageDialog(null, "Updated");
		    	}else{
		    		JOptionPane.showMessageDialog(null, "Update Failed");
		    	}
		    	
		    }
		}	
	}
	
	/**
	 * copies a source file to the papers folder
	 * @param src the source file
	 */
	private void copyFile(File src) {
    	InputStream inStream = null;
		OutputStream outStream = null;

		try{
			String fileName = src.getName();
			
		    File destFile =new File("src/Papers/"+fileName); //don't add a "/" in front
		    												 //of src, errors will
		    												 //ruin your life.
		 
		    	    inStream = new FileInputStream(src);
		    	    outStream = new FileOutputStream(destFile);
		    	    
		    	    byte[] buffer = new byte[1024];
		 
		    	    int length;
		    	    //copy the file content in bytes 
		    	    while ((length = inStream.read(buffer)) > 0){
		 
		    	    	outStream.write(buffer, 0, length);
		 
		    	    }
		 
		    	    inStream.close();
		    	    outStream.close();
		 
		    	}catch(IOException e){
		    		e.printStackTrace();
		    	}
		}
	

}
